function love.load()
	print("loaded mapeditor.")
	newobjmenu=false
	abouttosave=false
	fname="mymap"
	objects={ }
	lines={ }
	selected=0
	normselected=0
	moveselected=0
	direction=0
end
function love.update()
	if love.mouse.isDown("l") then
		if selected>0 then	
			objects[selected].x,objects[selected].y=love.mouse.getX(),love.mouse.getY()
		end
	end
end
function love.mousepressed(x,y,btn)
	if btn=="l" then
		normselected=0
		for k,v in pairs(objects) do
			if love.mouse.getX()<v.x+v.w and love.mouse.getX()>v.x then
				if love.mouse.getY()<v.y+v.h and love.mouse.getY()>v.y then
					selected=k
					normselected=k
				end
			end
			if v.t:sub(1,2)=="pl" then return end
			if love.mouse.getX()<v.x+50+v.w+50 and love.mouse.getX()>v.x+v.w+50 then
				if love.mouse.getY()<v.y+50+v.h+50 and love.mouse.getY()>v.y+50 then
					direction=1
					moveselected=k
					normselected=k
				end
			end
			if love.mouse.getX()<v.x+50+v.w+50 and love.mouse.getX()>v.x+v.w+50 then
				if love.mouse.getY()<v.y+100+v.h and love.mouse.getY()>v.y+v.h+50 then
					direction=2
					moveselected=k
					normselected=k
				end
			end
			if love.mouse.getX()<v.x+50 and love.mouse.getX()>v.x+100 then
				if love.mouse.getY()<v.y+100+v.h and love.mouse.getY()>v.y+v.h+50 then
					direction=3
					moveselected=k
					normselected=k
				end
			end
		end
		
		return
	end
	if btn=="r" then
		if love.mouse.isDown("l") then
			abouttosave=true
			return
		end
		if love.keyboard.isDown("f1") then
			objects[#objects+1]={t="btn",x=x,y=y,w=50,h=50,c={0,0,255,255},_a=function() end,a=function() end,n=function() end,_n=function() end,depth=#objects}
		end
		if love.keyboard.isDown("f2") then
			objects[#objects+1]={t="wll",x=x,y=y,w=50,h=50,c={0,255,0,255},depth=#objects}
		end
		if love.keyboard.isDown("f3") then
			objects[#objects+1]={t="pl1",x=x,y=y,w=64,h=64,c={0,120,180,255},depth=#objects}
		end
		if love.keyboard.isDown("f3") then
			objects[#objects+1]={t="pl1",x=x,y=y,w=64,h=64,c={0,120,255,255},depth=#objects}
		end
	end
end
function love.mousereleased(x,y,btn)
	if btn=="l" then selected=0 end
	if btn=="r" then abouttosave=false end
end
function love.keypressed(key)
	if key=="return" then
		if abouttosave then
			abouttosave=false
			-- encode file
			io.write("encoding map..\n")
			io.write("initializing datastream..\n")
			data=""
			for k,v in pairs(objects) do
				for d,s in pairs(v) do
					if d=="t" then
						data=data..s..":"
						io.write("$APPEND:"..s:upper())
					else
						data=data..d.."="..s..";"
						io.write(".")
					end
				end
				io.write("\n")
				data=data..";"
			end
			io.write("completed.\n")
			love.filesystem.write("maps/"..fname..".upm",data)
		end
	end
end
function love.draw()
	if abouttosave then
		love.graphics.setColor(255,0,0,50)
		love.graphics.rectangle("fill",0,0,640,480)
	end
	table.sort(objects,function(a,b)
		return a.depth<b.depth
	end )
	for k,v in pairs(objects) do
		love.graphics.setColor(v.c)
		love.graphics.rectangle("fill",v.x,v.y,v.w,v.h)
		if v.t:sub(1,2)=="pl" then return end
		if normselected==k then
		love.graphics.setColor(255,0,0,40)
		love.graphics.rectangle("fill",v.x+v.w+50,v.y+50,50,50)
		love.graphics.rectangle("fill",v.x+50,v.y+v.h+50,50,50)
		love.graphics.rectangle("fill",v.x+v.w+50,v.y+v.w+50,50,50)
		end
	end
end